<template>
    <div class="login-container">
        <el-card class="box-card">
            <template #header>
                <div class="card-header">
                    <span>账号登录</span>

                </div>
            </template>
            <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="120px">
                <el-form-item label="账号/邮箱/手机" prop="username">
                    <el-input v-model="loginForm.username" placeholder="请输入用户名，邮箱或手机"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="loginForm.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleLogin">登录</el-button>
                </el-form-item>
            </el-form>
            <div class="quick-login">
                <div>快速登录: </div>
                <el-button-group>
                    <el-icon>
                        <ChatRound />
                    </el-icon>
                    <label for="" style="color: pink;">微信登录</label>
                    <el-icon>
                        <ChatDotRound />
                    </el-icon>
                    <label for="" style="color: pink;">企业微信</label>
                </el-button-group>
            </div>
            <div class="form-options">
                    <input type="checkbox" id="auto-login">
                    <label class="auto-login">下次自动登录</label>
                </div>
            <div class="footer-links">
                <el-link href="#" underline="hover" @click="gethome">首页</el-link>
                <el-link href="#" underline="hover" @click="Tzregister">注册</el-link>
                <el-link href="#" underline="hover" @click="putpwd">忘记密码？</el-link>
            </div>
        </el-card>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import request from '@/request'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()

// 登录表单数据
const loginForm = reactive({
    username: '',
    password: '',
    captcha: ''
})

// 登录表单验证规则
const loginRules = reactive({
    username: [
        { required: true, message: '请输入登录名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码是8-20位字母、数字或特殊符号', trigger: 'blur' }
    ],
    captcha: [
        { required: true, message: '请输入图形验证码', trigger: 'blur' }
    ]
})

// 图形验证码图片地址，这里假设是后端接口，实际需替换
// const captchaSrc = ref('/api/getCaptcha') 
// const refreshCaptcha = () => {
//     // 刷新验证码逻辑，例如重新请求后端接口
//     console.log('刷新验证码')
// }
const userinfo = ref('')
const handleLogin = () => {
    // 这里添加登录逻辑，例如调用后端接口
    request({
        url: '/users/pwdlogin/', // 注册接口路径，根据实际情况调整
        method: 'POST',
        data: {
            username: loginForm.username,
            password: loginForm.password
        }
    }).then(resp => {
        console.log(resp.data);
        if (resp.data.code === 200) {
            ElMessage.success('登录成功')
            localStorage.setItem('username', userinfo.value);
            router.push('/personalcenter') // 登录成功后跳转到首页页
        } else {
            ElMessage.error(resp.data.message || '登录失败')
        }
    }).catch(error => {
        console.log(error)
        ElMessage.error('登录请求失败，请稍后重试')
    })
}


// 跳转修改密码
const putpwd = () => {
    router.push('/putpwd')
}
// 跳转首页
const gethome = () => {
    router.push('/')
}
// 跳转注册
const Tzregister = () => {
    router.push('/register')
}
</script>

<style scoped>
.login-container {
    max-width: 400px;
    margin: 50px auto;
    padding: 0 20px;
}

.box-card {
    padding: 20px;
    border-radius: 5px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}

.card-header {
    display: flex;
    justify-content: center;
    font-size: 20px;
    font-weight: bold;
}

.quick-login {
    display: flex;
    justify-content: center;
    margin: 20px 0;
}

.form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    font-size: 14px;
}

.forgot-link {
    color: gray;
}

.auto-login {
    margin-right: 100px;
    color: gray;
}


.footer-links {
    display: flex;
    justify-content: center;
    gap: 20px;
}
</style>